/**
 * Copyright (c) 2010 CWI Amsterdam and others. All rights reserved.
 * This program and the accompanying materials are made available
 * under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     CWI Amsterdam - initial API and implementation
 *
 * $Id$
 */
package nl.cwi.arne.impl;

import java.util.Collection;

import nl.cwi.arne.Edge;
import nl.cwi.arne.Network;
import nl.cwi.arne.Node;
import nl.cwi.arne.NodeActivation;
import nl.cwi.arne.factory.ArnePackage;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.MinimalEObjectImpl;
import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.EObjectWithInverseResolvingEList;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.InternalEList;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Node</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link nl.cwi.arne.impl.NodeImpl#getName <em>Name</em>}</li>
 *   <li>{@link nl.cwi.arne.impl.NodeImpl#getLevels <em>Levels</em>}</li>
 *   <li>{@link nl.cwi.arne.impl.NodeImpl#getStartLevel <em>Start Level</em>}</li>
 *   <li>{@link nl.cwi.arne.impl.NodeImpl#getIncoming <em>Incoming</em>}</li>
 *   <li>{@link nl.cwi.arne.impl.NodeImpl#getOutgoing <em>Outgoing</em>}</li>
 *   <li>{@link nl.cwi.arne.impl.NodeImpl#getNetwork <em>Network</em>}</li>
 *   <li>{@link nl.cwi.arne.impl.NodeImpl#getActivations <em>Activations</em>}</li>
 *   <li>{@link nl.cwi.arne.impl.NodeImpl#isMonotonic <em>Monotonic</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class NodeImpl extends MinimalEObjectImpl implements Node {
	
	/**
	 * The default value of the '{@link #getName() <em>Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getName()
	 * @generated
	 * @ordered
	 */
	protected static final String NAME_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getName() <em>Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getName()
	 * @generated
	 * @ordered
	 */
	protected String name = NAME_EDEFAULT;

	/**
	 * The default value of the '{@link #getLevels() <em>Levels</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getLevels()
	 * @generated
	 * @ordered
	 */
	protected static final byte LEVELS_EDEFAULT = 0x02;

	/**
	 * The cached value of the '{@link #getLevels() <em>Levels</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getLevels()
	 * @generated
	 * @ordered
	 */
	protected byte levels = LEVELS_EDEFAULT;

	/**
	 * The default value of the '{@link #getStartLevel() <em>Start Level</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getStartLevel()
	 * @generated
	 * @ordered
	 */
	protected static final byte START_LEVEL_EDEFAULT = (byte)0xff;

	/**
	 * The cached value of the '{@link #getStartLevel() <em>Start Level</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getStartLevel()
	 * @generated
	 * @ordered
	 */
	protected byte startLevel = START_LEVEL_EDEFAULT;

	/**
	 * The cached value of the '{@link #getIncoming() <em>Incoming</em>}' reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getIncoming()
	 * @generated
	 * @ordered
	 */
	protected EList<Edge> incoming;

	/**
	 * The cached value of the '{@link #getOutgoing() <em>Outgoing</em>}' reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getOutgoing()
	 * @generated
	 * @ordered
	 */
	protected EList<Edge> outgoing;

	/**
	 * The cached value of the '{@link #getActivations() <em>Activations</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getActivations()
	 * @generated
	 * @ordered
	 */
	protected EList<NodeActivation> activations;

	/**
	 * The default value of the '{@link #isMonotonic() <em>Monotonic</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isMonotonic()
	 * @generated
	 * @ordered
	 */
	protected static final boolean MONOTONIC_EDEFAULT = true;

	/**
	 * The cached value of the '{@link #isMonotonic() <em>Monotonic</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isMonotonic()
	 * @generated
	 * @ordered
	 */
	protected boolean monotonic = MONOTONIC_EDEFAULT;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected NodeImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return ArnePackage.Literals.NODE;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getName() {
		return name;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setName(String newName) {
		String oldName = name;
		name = newName;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ArnePackage.NODE__NAME, oldName, name));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public byte getLevels() {
		return levels;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setLevels(byte newLevels) {
		byte oldLevels = levels;
		levels = newLevels;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ArnePackage.NODE__LEVELS, oldLevels, levels));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public byte getStartLevel() {
		return startLevel;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setStartLevel(byte newStartLevel) {
		byte oldStartLevel = startLevel;
		startLevel = newStartLevel;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ArnePackage.NODE__START_LEVEL, oldStartLevel, startLevel));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Edge> getIncoming() {
		if (incoming == null) {
			incoming = new EObjectWithInverseResolvingEList<Edge>(Edge.class, this, ArnePackage.NODE__INCOMING, ArnePackage.EDGE__TARGET);
		}
		return incoming;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Edge> getOutgoing() {
		if (outgoing == null) {
			outgoing = new EObjectWithInverseResolvingEList<Edge>(Edge.class, this, ArnePackage.NODE__OUTGOING, ArnePackage.EDGE__SOURCE);
		}
		return outgoing;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Network getNetwork() {
		if (eContainerFeatureID() != ArnePackage.NODE__NETWORK) return null;
		return (Network)eContainer();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetNetwork(Network newNetwork, NotificationChain msgs) {
		msgs = eBasicSetContainer((InternalEObject)newNetwork, ArnePackage.NODE__NETWORK, msgs);
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setNetwork(Network newNetwork) {
		if (newNetwork != eInternalContainer() || (eContainerFeatureID() != ArnePackage.NODE__NETWORK && newNetwork != null)) {
			if (EcoreUtil.isAncestor(this, newNetwork))
				throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
			NotificationChain msgs = null;
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			if (newNetwork != null)
				msgs = ((InternalEObject)newNetwork).eInverseAdd(this, ArnePackage.NETWORK__NODES, Network.class, msgs);
			msgs = basicSetNetwork(newNetwork, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ArnePackage.NODE__NETWORK, newNetwork, newNetwork));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<NodeActivation> getActivations() {
		if (activations == null) {
			activations = new EObjectContainmentEList<NodeActivation>(NodeActivation.class, this, ArnePackage.NODE__ACTIVATIONS);
		}
		return activations;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public boolean isMonotonic() {
		return monotonic;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setMonotonic(boolean newMonotonic) {
		boolean oldMonotonic = monotonic;
		monotonic = newMonotonic;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ArnePackage.NODE__MONOTONIC, oldMonotonic, monotonic));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case ArnePackage.NODE__INCOMING:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getIncoming()).basicAdd(otherEnd, msgs);
			case ArnePackage.NODE__OUTGOING:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getOutgoing()).basicAdd(otherEnd, msgs);
			case ArnePackage.NODE__NETWORK:
				if (eInternalContainer() != null)
					msgs = eBasicRemoveFromContainer(msgs);
				return basicSetNetwork((Network)otherEnd, msgs);
		}
		return super.eInverseAdd(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case ArnePackage.NODE__INCOMING:
				return ((InternalEList<?>)getIncoming()).basicRemove(otherEnd, msgs);
			case ArnePackage.NODE__OUTGOING:
				return ((InternalEList<?>)getOutgoing()).basicRemove(otherEnd, msgs);
			case ArnePackage.NODE__NETWORK:
				return basicSetNetwork(null, msgs);
			case ArnePackage.NODE__ACTIVATIONS:
				return ((InternalEList<?>)getActivations()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eBasicRemoveFromContainerFeature(NotificationChain msgs) {
		switch (eContainerFeatureID()) {
			case ArnePackage.NODE__NETWORK:
				return eInternalContainer().eInverseRemove(this, ArnePackage.NETWORK__NODES, Network.class, msgs);
		}
		return super.eBasicRemoveFromContainerFeature(msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case ArnePackage.NODE__NAME:
				return getName();
			case ArnePackage.NODE__LEVELS:
				return getLevels();
			case ArnePackage.NODE__START_LEVEL:
				return getStartLevel();
			case ArnePackage.NODE__INCOMING:
				return getIncoming();
			case ArnePackage.NODE__OUTGOING:
				return getOutgoing();
			case ArnePackage.NODE__NETWORK:
				return getNetwork();
			case ArnePackage.NODE__ACTIVATIONS:
				return getActivations();
			case ArnePackage.NODE__MONOTONIC:
				return isMonotonic();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case ArnePackage.NODE__NAME:
				setName((String)newValue);
				return;
			case ArnePackage.NODE__LEVELS:
				setLevels((Byte)newValue);
				return;
			case ArnePackage.NODE__START_LEVEL:
				setStartLevel((Byte)newValue);
				return;
			case ArnePackage.NODE__INCOMING:
				getIncoming().clear();
				getIncoming().addAll((Collection<? extends Edge>)newValue);
				return;
			case ArnePackage.NODE__OUTGOING:
				getOutgoing().clear();
				getOutgoing().addAll((Collection<? extends Edge>)newValue);
				return;
			case ArnePackage.NODE__NETWORK:
				setNetwork((Network)newValue);
				return;
			case ArnePackage.NODE__ACTIVATIONS:
				getActivations().clear();
				getActivations().addAll((Collection<? extends NodeActivation>)newValue);
				return;
			case ArnePackage.NODE__MONOTONIC:
				setMonotonic((Boolean)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case ArnePackage.NODE__NAME:
				setName(NAME_EDEFAULT);
				return;
			case ArnePackage.NODE__LEVELS:
				setLevels(LEVELS_EDEFAULT);
				return;
			case ArnePackage.NODE__START_LEVEL:
				setStartLevel(START_LEVEL_EDEFAULT);
				return;
			case ArnePackage.NODE__INCOMING:
				getIncoming().clear();
				return;
			case ArnePackage.NODE__OUTGOING:
				getOutgoing().clear();
				return;
			case ArnePackage.NODE__NETWORK:
				setNetwork((Network)null);
				return;
			case ArnePackage.NODE__ACTIVATIONS:
				getActivations().clear();
				return;
			case ArnePackage.NODE__MONOTONIC:
				setMonotonic(MONOTONIC_EDEFAULT);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case ArnePackage.NODE__NAME:
				return NAME_EDEFAULT == null ? name != null : !NAME_EDEFAULT.equals(name);
			case ArnePackage.NODE__LEVELS:
				return levels != LEVELS_EDEFAULT;
			case ArnePackage.NODE__START_LEVEL:
				return startLevel != START_LEVEL_EDEFAULT;
			case ArnePackage.NODE__INCOMING:
				return incoming != null && !incoming.isEmpty();
			case ArnePackage.NODE__OUTGOING:
				return outgoing != null && !outgoing.isEmpty();
			case ArnePackage.NODE__NETWORK:
				return getNetwork() != null;
			case ArnePackage.NODE__ACTIVATIONS:
				return activations != null && !activations.isEmpty();
			case ArnePackage.NODE__MONOTONIC:
				return monotonic != MONOTONIC_EDEFAULT;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (name: ");
		result.append(name);
		result.append(", levels: ");
		result.append(levels);
		result.append(", startLevel: ");
		result.append(startLevel);
		result.append(", monotonic: ");
		result.append(monotonic);
		result.append(')');
		return result.toString();
	}

} //NodeImpl
